package com.jingdianjichi.subject.common.context;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LoginContextHolder {
    private static final InheritableThreadLocal<Map<String, Object>> THREAD_LOCAL = new InheritableThreadLocal<>();

    public static void set(String key, Object value){
        //解决两个问题1、怎么把map放到ThreadLocal中，2、map中有值怎么复用(不会覆盖原来的kv)
        Map<String, Object> map = getThreadLocalMap();
        map.put(key, value);
    }
    public static Object get(String key){
        return getThreadLocalMap().get(key);
    }
    public static void remove(){
        THREAD_LOCAL.remove();
    }

    public static String getLoginId(){
        String loginId = (String)getThreadLocalMap().get("loginId");
        return loginId;
    }

    public static Map<String, Object> getThreadLocalMap(){
        Map<String, Object> map = THREAD_LOCAL.get();
        if (map == null){
            map = new ConcurrentHashMap<>();
            THREAD_LOCAL.set(map);
        }
        return map;
    }
}
